<?php
/**
 * Created by PhpStorm.
 * User: 李洋波
 * Date: 2016/1/7
 * Time: 20:15
 */
class DepositApi extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('users_model');
        $this->load->model('api_model');
    }

    /**
     * 预存款API
     */
    public function PostDepositRecord()
    {
        $gp = gp();
        if($gp['data']) {
            if ($gp['access_token']) {//验证access_token参数是否存在
                //查询当前app信息
                $ApiInfoData = $this->api_model->GetApiInfo($gp['access_token']);
                //检测当前appid状态
                switch ($ApiInfoData['status']) {
                    case '1':
                        echo json_encode(array('msg' => 'appInfo Audit ing....', 'code' => -40002));
                        break;
                    case '2':
                        //检测其APP类型
                        if ($ApiInfoData['apitype'] == "DepositRecord") {
                            $tokenTime = time() - $ApiInfoData['tokentime'];
                            //超时时间
                            $Timeout = 7200;
                            //验证access_token是否超时
                            if ($tokenTime < $Timeout) {
                                //允许POST的数据键名
                                $allowDepositRecordPostKey = array('currency','money', 'time','customers_id','orderId','transactionId');
                                //必须POST的数据键名
                                $mustDepositRecordPostKey = array('customers_id', 'money','currency','time','transactionId');
                                //验证POST数据是否包含非法项
                                $CheckPostKey = array_diff(array_keys($gp['data']), $allowDepositRecordPostKey);
                                //验证Post数据是否有遗漏项
                                $CheckPostKey2 = array_diff($mustDepositRecordPostKey, array_keys($gp['data']));
                                if ($CheckPostKey) {
                                    echo json_encode(array('msg' => 'include Illegal Item', 'Item' => implode(',',$CheckPostKey), 'code' => -40007));
                                    exit();
                                }
                                if ($CheckPostKey2) {
                                    echo json_encode(array('msg' => 'lack item', 'Item' => implode(',',$CheckPostKey2), 'code' => -40008));
                                    exit();
                                }
                                //验证各项值是否填写
                                if ($gp['data']['customers_id'] == '') {
                                    echo json_encode(array('msg' => 'customers_id is null!', 'code' => -40009));
                                    exit();
                                }
                                if ($gp['data']['transactionId'] == '') {
                                    echo json_encode(array('msg' => 'transactionId is null!', 'code' => -40088));
                                    exit();
                                }
                                if ($gp['data']['money'] == '') {
                                    echo json_encode(array('msg' => 'money is null!', 'code' => -40010));
                                    exit();
                                }
                                if (!is_numeric($gp['data']['money'])) {
                                    echo json_encode(array('msg' => 'money field format error (double)!', 'code' => -40011));
                                    exit();
                                }
                                if ($gp['data']['time'] == '') {
                                    echo json_encode(array('msg' => 'time is null', 'code' => -40012));
                                    exit();
                                }
                                if(!is_numeric($gp['data']['time'])){
                                    echo json_encode(array('msg' => 'time field format error (int)!', 'code' => -40013));
                                    exit();
                                }
                                if ($gp['data']['currency'] == '') {
                                    echo json_encode(array('msg' => 'currency is null!', 'code' => -40016));
                                    exit();
                                }
                                if (is_numeric($gp['data']['currency']) || strlen($gp['data']['currency']) > 6) {
                                    echo json_encode(array('msg' => 'currency Illegal!', 'code' => -40017));
                                    exit();
                                }
                                //入库记录加减款
                                $Record_id = $this->users_model->AddDepositRecord($gp['data']);
                                if ($Record_id) {
                                    $this->users_model->UpdateBalances($gp['data']['customers_id'],$gp['data']['money'],'Deposit');
                                    echo json_encode(array('msg' => 'OK', 'code' => 1));
                                } else {
                                    echo json_encode(array('msg' => 'System error ' . $gp['data']['email'] . ' post failure', 'code' => -40015));
                                }
                            } else {
                                echo json_encode(array('msg' => 'access_token Illegal  or Time out!', 'code' => -40006));
                                exit();
                            }
                        } else {
                            echo json_encode(array('msg' => 'app type error!', 'code' => -40005));
                            exit();
                        }
                        break;
                    case '3':
                        echo json_encode(array('msg' => 'appInfo by prohibit!', 'code' => -40003));
                        break;
                    default:
                        echo json_encode(array('msg' => 'appInfo Illegal!', 'code' => -40004));
                        break;
                }
            } else {
                echo json_encode(array('msg' => 'access_token is null!', 'code' => -40001));
                exit();
            }
        }else{
            echo json_encode(array('msg' => 'data is null!', 'code' => -40000));
            exit();
        }
    }

    /**
     * 获取交易记录
     */
    public function GetDepositRecord(){
        $gp=gp();
        if($gp['access_token']) {
            //查询当前app信息
            $ApiInfoData = $this->api_model->GetApiInfo($gp['access_token']);
            if($ApiInfoData['access_token']) {
                //检测当前appid状态
                switch ($ApiInfoData['status']) {
                    case '1':
                        echo json_encode(array('msg' => 'appInfo Audit ing....', 'code' => -50002));
                        break;
                    case '2':
                        //检测其APP类型
                        if ($ApiInfoData['apitype'] == "DepositRecord") {
                            $tokenTime = time() - $ApiInfoData['tokentime'];
                            //超时时间
                            $Timeout = 7200;
                            //验证access_token是否超时
                            if ($tokenTime < $Timeout) {
                                $data = $this->users_model->depositlistrecord('',$gp['customers_id']);
                                echo json_encode($data);
                            } else {
                                echo json_encode(array('msg' => 'access_token Illegal  or Time out!', 'code' => -40006));
                                exit();
                            }
                        }
                        break;
                    case '3':
                        echo json_encode(array('msg' => 'appInfo by prohibit!', 'code' => -50003));
                        break;
                    default:
                        echo json_encode(array('msg' => 'appInfo Illegal!', 'code' => -50004));
                        break;
                }
            }else{
                echo json_encode(array('msg' => 'access_token Illegal!', 'code' => -50005));
                exit();
            }
        }else{
            echo json_encode(array('msg' => 'access_token is null!', 'code' => -50001));
            exit();
        }
    }
    public function GetBalance(){
        $gp=gp();
        if($gp['access_token']) {
            //查询当前app信息
            $ApiInfoData = $this->api_model->GetApiInfo($gp['access_token']);
            if($ApiInfoData['access_token']) {
                //检测当前appid状态
                switch ($ApiInfoData['status']) {
                    case '1':
                        echo json_encode(array('msg' => 'appInfo Audit ing....', 'code' => -60002));
                        break;
                    case '2':
                        //检测其APP类型
                        if ($ApiInfoData['apitype'] == "DepositRecord") {
                            $tokenTime = time() - $ApiInfoData['tokentime'];
                            //超时时间
                            $Timeout = 7200;
                            //验证access_token是否超时
                            if ($tokenTime < $Timeout) {
                                $data = $this->users_model->GetBalance($gp['customers_id']);
                                echo json_encode($data);
                            } else {
                                echo json_encode(array('msg' => 'access_token Illegal  or Time out!', 'code' => -60006));
                                exit();
                            }
                        }
                        break;
                    case '3':
                        echo json_encode(array('msg' => 'appInfo by prohibit!', 'code' => -60003));
                        break;
                    default:
                        echo json_encode(array('msg' => 'appInfo Illegal!', 'code' => -60004));
                        break;
                }
            }else{
                echo json_encode(array('msg' => 'access_token Illegal!', 'code' => -60005));
                exit();
            }
        }else{
            echo json_encode(array('msg' => 'access_token is null!', 'code' => -60001));
            exit();
        }
    }
}